<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="投票标题" prop="voteTitle">
        <el-input
          v-model="queryParams.voteTitle"
          placeholder="请输入投票标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:voteInfo:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:voteInfo:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:voteInfo:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:voteInfo:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="voteInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="投票ID" align="center" prop="voteId" />
      <el-table-column label="投票标题" align="center" prop="voteTitle" />
      <el-table-column label="投票描述" align="center" prop="voteDesc" />
      <el-table-column
        label="开始时间"
        align="center"
        prop="startTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="vote_info_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            v-if="scope.row.status != 2"
            link
            type="primary"
            icon="Plus"
            @click="handleOptionAdd(scope.row)"
            v-hasPermi="['business:voteOption:add']"
            >新增投票选项</el-button
          >
           <el-button
            link
            type="warning"
            icon="View"
            @click="handleOptionView(scope.row)"
            v-hasPermi="['business:voteOption:query']"
            >查看投票选项</el-button
          >
          <el-button
            v-if="scope.row.status != 2"
            link
            type="success"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:voteInfo:edit']"
            >修改</el-button
          >
          <el-button
            v-if="scope.row.status == 0"
            link
            type="warning"
            icon="Warning"
            @click="handleCheck(scope.row)"
            v-hasPermi="['business:voteInfo:checkout']"
            >审核</el-button
          >

          <el-button
            link
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:voteInfo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投票信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="voteInfoRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="投票标题" prop="voteTitle">
          <el-input
            v-model="form.voteTitle"
            placeholder="请输入投票标题"
            :disabled="form.status == 1"
          />
        </el-form-item>
        <el-form-item label="投票描述" prop="voteDesc">
          <el-input
            v-model="form.voteDesc"
            type="textarea"
            placeholder="请输入内容"
           
          />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            clearable
            v-model="form.startTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择开始时间"
            :disabled="form.status == 1"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            clearable
            v-model="form.endTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择结束时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
      <!-- 添加投票选项对话框 -->
  <el-dialog :title="optionTitle" v-model="optionOpen" width="500px" append-to-body>
    <el-form
      ref="voteOptionRef"
      :model="optionForm"
      :rules="optionRules"
      label-width="80px"
    >
      <el-form-item label="所属投票" prop="voteTitle">
       {{ optionForm.voteTitle }}
      </el-form-item>
      <el-form-item label="选项内容" prop="optionContent">
        <editor v-model="optionForm.optionContent" :min-height="192" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="optionSubmitForm">确 定</el-button>
        <el-button @click="optionCancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
  </div>

</template>

<script setup name="VoteInfo">
import {
  listVoteInfo,
  getVoteInfo,
  delVoteInfo,
  addVoteInfo,
  updateVoteInfo,
  updateVoteInfoStatus,
} from "@/api/business/voteInfo";
import { addVoteOption } from "@/api/business/voteOption";
import {useRouter} from 'vue-router'

const { proxy } = getCurrentInstance();
const { vote_info_status } = proxy.useDict("vote_info_status");

const router = useRouter()
const voteInfoList = ref([]);
const open = ref(false);
const optionOpen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const optionTitle=ref('')

const optionData = reactive({
  optionForm: {},
  optionRules: {
    voteId: [{ required: true, message: "所属投票不能为空", trigger: "blur" }],
    voteTitle: [{ required: true, message: "选项标题不能为空", trigger: "blur" }],
    optionContent: [
      {
        required: true,
        validator: (rule, value, callback) => {
          if (!value) {
            return callback(new Error("选项内容不能为空"));
          }
          callback();
        },
        trigger: "blur",
      },
    ],
  },
});
const { optionForm, optionRules } = toRefs(optionData);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    voteTitle: null,
    voteDesc: null,
    startTime: null,
    endTime: null,
    status: null,
  },
  rules: {
    voteTitle: [
      { required: true, message: "投票标题不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询投票信息列表 */
function getList() {
  loading.value = true;
  listVoteInfo(queryParams.value).then((response) => {
    voteInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    voteId: null,
    voteTitle: null,
    voteDesc: null,
    startTime: null,
    endTime: null,
    status: null,
    createBy: null,
    createTime: null,
  };
  proxy.resetForm("voteInfoRef");
}
// 表单重置
function resetOption() {
  form.value = {
    voteId: null,
    voteTitle: null,
    optionContent:null

  };
  proxy.resetForm("voteOptionRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.voteId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加投票信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _voteId = row.voteId || ids.value;
  getVoteInfo(_voteId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改投票信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["voteInfoRef"].validate((valid) => {
    if (valid) {
      if (form.value.voteId != null) {
        updateVoteInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addVoteInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 审核按钮操作 */
function handleCheck(row) {
  const _voteId = row.voteId;
  proxy.$modal
    .confirm('是否要审核投票信息编号为"' + _voteId + '"的数据项？')
    .then(function () {
      row.status = 1;
      updateVoteInfoStatus(row).then((response) => {
        console.log(response);
        getList();
        proxy.$modal.msgSuccess("审核成功");
      });
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "business/voteInfo/export",
    {
      ...queryParams.value,
    },
    `voteInfo_${new Date().getTime()}.xlsx`
  );
}
const handleOptionAdd = (row) => {
  optionTitle.value="添加投票选项信息"
  optionOpen.value = true;
  optionForm.value.voteId = row.voteId;
  optionForm.value.voteTitle = row.voteTitle;
  resetOption()
};
getList();
function optionSubmitForm() {
  proxy.$refs["voteOptionRef"].validate((valid) => {
    if (valid) {
      addVoteOption(optionForm.value).then((response) => {
        proxy.$modal.msgSuccess("新增成功");
        optionOpen.value = false;
        getList();
      });
    }
  });
}
function optionCancel() {
  optionOpen.value = false;
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _voteId = row.voteId || ids.value
  proxy.$modal.confirm('是否确认删除投票信息编号为"' + _voteId + '"的数据项？').then(function() {
    return delVoteInfo(_voteId)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}
function handleOptionView(row){
  router.push({path:"/business/voteOption",query:{voteTitle:row.voteTitle}});
}
</script>
